package edu.mju.ddc.controller.sysadmin;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import edu.mju.ddc.domain.Manager;
import edu.mju.ddc.service.ManagerService;
import edu.mju.ddc.service.ManagerServiceImpl;

public class SecurityMgr extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SecurityMgr() {
		super();
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("UTF-8");

		response.setCharacterEncoding("UTF-8");
		
		ManagerService managerService = null;
		Manager manager = null;
		
		String act = request.getParameter("act");
		if ("".equals(act) || act == null || "toLogin".equals(act)){
			request.getRequestDispatcher("../sysadmin/login.jsp").forward(request, response);
		} else if("login".equals(act.trim())){ 
			String loginId = request.getParameter("loginid").trim();
			String loginPwd = request.getParameter("loginpwd");
			
			String msg = "";
			
			managerService = new ManagerServiceImpl();
			manager = managerService.loginCheck(loginId);
			
			if (manager == null) {
				msg = "您输入的用户不存在！";
				
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("../sysadmin/login.jsp").forward(request, response);
			} else if (!manager.getLoginPwd().equals(loginPwd)) {
				msg = "您输入的用户名/密码错误！";
				
				request.setAttribute("msg", msg);
				request.getRequestDispatcher("../sysadmin/login.jsp").forward(request, response);
			} else {
				request.getSession().setAttribute("loginedMgr", manager);
				response.sendRedirect("securityMgr?act=mainscreen");
			}
			
		} else if ("mainscreen".equals(act.trim())) {
//			HttpSession session = request.getSession(true);
//			session.setAttribute("loginedMgr", manager);
			
			request.getRequestDispatcher("../sysadmin/main.jsp").forward(request, response);
		} else if("logout".equals(act)){
			HttpSession session = request.getSession();
			session.removeAttribute("loginedMgr"); //remove the logined manager from the session.
			session.invalidate();  //destroy the session 
			response.sendRedirect("securityMgr?act=toLogin");
		}
		
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doGet(request, response);
	}

}
